using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Xeml.Document;
using Xeml.Document.EnvironmentalOntology;
using Xeml.Document.Contracts;

namespace Xid.Controls.ParameterEditCtrls
{
    public partial class CycleEntryEditCtrl : UserControl
    {
        private IDynamicOntologyTerm _paramSpec;
        private string _context;

        private DynamicValue v;

        public DynamicValue Value
        {
            get { return v; }
            set 
            { 
                v = value; 
                SetupValue(); 
            }
        }

        private void SetupValue()
        {
            if (v != null)
            {
                this.timeSpanPicker1.DataBindings.Add("Value",v, "Time", true, DataSourceUpdateMode.OnPropertyChanged);
                switch (this.edtMode)
                {
                    case EditMode.ComboValue:
                        {
                            this.comboBoxValue.DataBindings.Add("Text", v, "Val", true, DataSourceUpdateMode.OnPropertyChanged);
                            break;
                        }
                    case EditMode.ComboListValue:
                        {
                            this.comboBoxValue.DataBindings.Add("SelectedValue", v, "Val", true, DataSourceUpdateMode.OnPropertyChanged);
                            break;
                        }
                    case EditMode.NumericValue:
                        {
                            this.numericUpDownValue.DataBindings.Add("Value", v, "DoubleValue", true, DataSourceUpdateMode.OnPropertyChanged);
                            break;
                        }
                    case EditMode.BooleanValue:
                        {
                            //  this.checkBox.DataBindings.Add("Checked", _currentValue, "BooleanValue", true, DataSourceUpdateMode.OnPropertyChanged);
                            this.comboBoxValue.DataBindings.Add("SelectedValue", v, "BooleanValue", true, DataSourceUpdateMode.OnPropertyChanged);

                            //this.numericUpDownValue.Value = (decimal)((Value)this._currentValue).DoubleValue;
                            break;
                        }
                     default:
                        break;
                }
            }
        }


        public bool EnableDrop
        {
            get { return kryptonButtonDrop.Enabled; }
            set { kryptonButtonDrop.Enabled = value; }
        }
	


        public CycleEntryEditCtrl()
        {
            InitializeComponent();
            this.timeSpanPicker1.Height = 21;
            this.kryptonButtonDrop.Enabled = false;
        }

        private EditMode edtMode;
        public void SetupSpec(IDynamicOntologyTerm spec, string context)
        {
           
            _paramSpec = spec;
            _context = context;
            if (!string.IsNullOrEmpty(_context))
            {
                this.kryptonButtonDrop.Enabled = true;
                if (_paramSpec.GetEnums(_context).Count > 0)
                {
                    this.edtMode = EditMode.ComboListValue;
                    comboBoxValue.Visible = true;
                    numericUpDownValue.Visible = false;
                    comboBoxValue.DropDownStyle = ComboBoxStyle.DropDownList;
                    
                    
                    comboBoxValue.DataSource = _paramSpec.GetEnums(_context);


                    comboBoxValue.DisplayMember = "Key";
                    comboBoxValue.ValueMember = "Value";
                    return;
                }
                else if (_paramSpec.GetDataType(_context) == VariableDataTypes.Number)
                {
                    this.edtMode = EditMode.NumericValue;
                    comboBoxValue.Visible = false;
                    numericUpDownValue.Visible = true;
                    numericUpDownValue.DecimalPlaces = 2;
                    decimal? mi = _paramSpec.GetMin(_context);
                    decimal? mx = _paramSpec.GetMax(_context);
                    if (mi != null)
                        numericUpDownValue.Minimum = (decimal)mi;
                    else
                        numericUpDownValue.Minimum = Decimal.MinValue;
                    if (mx != null)
                        numericUpDownValue.Maximum = (decimal)mx;
                    else
                        numericUpDownValue.Maximum = Decimal.MaxValue;

                    return;
                }
                else if (_paramSpec.GetDataType(_context) == VariableDataTypes.Bool)
                {
                    this.edtMode = EditMode.BooleanValue;
                    //BindingList<bool> bl = new BindingList<bool>();
                    //bl.Add(true);
                    //bl.Add(false);
                    List<KeyValuePair<bool, bool>> bl = new List<KeyValuePair<bool, bool>>();

                    bl.Add(new KeyValuePair<bool, bool>(true, true));
                    bl.Add(new KeyValuePair<bool, bool>(false, false));

                    comboBoxValue.Visible = true;
                    numericUpDownValue.Visible = false;
                    comboBoxValue.DropDownStyle = ComboBoxStyle.DropDownList;
                    comboBoxValue.DataSource = bl;
                    comboBoxValue.DisplayMember = "Key";
                    comboBoxValue.ValueMember = "Value";
                    return;
                }

                else
                {
                    this.edtMode = EditMode.ComboValue;
                    comboBoxValue.Visible = true;
                    numericUpDownValue.Visible = false;
                    comboBoxValue.DropDownStyle = ComboBoxStyle.DropDown;
                }
            }
            else
            {
                this.edtMode = EditMode.ComboValue;
                comboBoxValue.Visible = true;
                numericUpDownValue.Visible = false;
                comboBoxValue.DropDownStyle = ComboBoxStyle.DropDown;
            }
        }

        private void kryptonButton1_Click(object sender, EventArgs e)
        {
            if (DropMe != null)
                DropMe(this, new EventArgs());
        }

        public event EventHandler DropMe;
    }
}
